package edu.ynu.se.xiecheng.achitectureclass.common.controller;
import edu.ynu.se.xiecheng.achitectureclass.common.entity.Image;
import edu.ynu.se.xiecheng.achitectureclass.common.service.CameraService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/camera")
public class CameraController {

    @Autowired
    private CameraService cameraService; // 注入 CameraService 服务

    @PostMapping("/capture")
    public ResponseEntity<String> captureImage(@RequestParam String userId,
                                                @RequestParam String deviceSerial) {
        try {
            // 调用 CameraService 服务方法进行抓拍并保存图片
            cameraService.captureCameraImage(userId, deviceSerial);
            return ResponseEntity.ok("Image captured and saved successfully!");
        } catch (IOException e) {
            e.printStackTrace();
            return ResponseEntity.status(500).body("Error occurred while saving the image.");
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.status(400).body("Failed to capture image.");
        }
    }

    // 获取所有抓取的图片，按时间排序
    @GetMapping("/images")
    public ResponseEntity<List<Image>> getAllCapturedImages() {
        List<Image> images = cameraService.getAllCapturedImages();
        return ResponseEntity.ok(images);  // 返回图片数据
    }
}
